package com.csaba.connector.dummy;

import java.util.Date;
import java.util.logging.Logger;

import com.csaba.connector.I18NServiceException;
import com.csaba.connector.ServiceException;
import com.csaba.connector.dummy.model.DummyBank;
import com.csaba.connector.model.Amount;
import com.csaba.connector.model.HistoryItem;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.AbstractAccountHistoryService;
import com.csaba.util.LoggerHelper;

public class DummyAccountHistoryService extends AbstractAccountHistoryService
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	@Override
	public void execute( final Session session ) throws ServiceException
	{
		if ( account == null || from == null || to == null )
			throw new I18NServiceException(DummyBank.getInstance(), "msg.incompleteInput", "", getClass().getName());

		logger.info("Dummy: history for account " + account + " generated.");
		Codes.waitABit();
		int count = (int) ( Math.random() * 20 ) + 2;
		double balance = account.getBookedBalance().getAmount();
		final long diff = ( to.getTime() - from.getTime() ) / count;
		historyItems = new HistoryItem[count];
		final Date date = (Date) to.clone();

		for ( count--; count >= 0; count-- )
		{
			final HistoryItem item = new HistoryItem();
			item.setOwner(account);
			item.setDescription("Dummy transaction " + count);
			final Amount amount = new Amount(1000 - (int) ( Math.random() * 2000 ), account.getCurrency());
			item.setAmount(amount);
			item.setBalance(new Amount(balance, account.getCurrency()));
			balance -= amount.getAmount();
			date.setTime(date.getTime() - diff);
			item.setDate((Date) date.clone());
			item.setRemoteProperty("partner.account", "1234556767");
			item.setRemoteProperty("partner.name", "John Dozer");

			historyItems[count] = item;
		}

	}
}
